import {Global} from '../../mongodb';
import {ApiCall} from 'tsrpc';
import {ReqAdd, ResAdd} from '../../shared/protocols/teams/PtlAdd';

export default async function (call: ApiCall<ReqAdd, ResAdd>) {
    //过期时间
    const effDate = call.req.effDate;
    const now = new Date();
    //设置创建时间
    call.req.query.createTime = now;
    //在当前日期上添加对应的时间等于过期时间
    if (effDate == 'oneYear') {
        call.req.query.expTime = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    } else {
        //设置为2099年
        call.req.query.expTime = new Date(2099, 0, 1);
    }
    //设置默认头像
    call.req.query.avatar = '/icon/human.png';


    const ret = await Global.db.collection('teams').insertOne(call.req.query);
    return call.succ({newID: ret.insertedId.toString()});

}